package ua.kpi.javatraning5.dgrishchenko.module1;

/**
 * Class for find Polyndrom
 * 
 * @author Grishchenko Dmytro
 * @version 1.0
 * 
 */
public class Polyndrom {

	/**
	 * Minimum length polyndrom word
	 */
	static final int MIN_lENGTH = 3;
	public static String[] polyndromArray;
	static int counter = 0;

	public static void main(String args[]) {

		checkArguments(args);
		polyndromArray = new String[args[0].length()];
		String word = args[0];
		word = delleteSpaces(word);
		find(word);
		show(polyndromArray);
	}

	/**
	 * 
	 * @param args
	 *            It's argument which checking in command line.
	 */
	public static void checkArguments(String[] args) {
		if ((args.length == 0)) {
			throw new IllegalArgumentException(
					"Bad arguments, enter correct arguments");

		} else {
			if (args[0].length() < 3) {
				throw new IllegalArgumentException(
						"Bad arguments, enter correct arguments");

			}
		}
	}

	/**
	 * 
	 * @param word
	 *            the word without spaces
	 */
	static void find(String word) {
		if (word.length() < MIN_lENGTH) {
			return;
		}
		String backword = "";
		word = word.toLowerCase();
		for (int i = word.length() - 1; i >= 0; i--) {
			backword += (char) word.codePointAt(i);
		}
		if (word.equals(backword)) {
			polyndromArray[counter++] = word;
		}
		find(word.substring(1, word.length() - 1));
	}

	/**
	 * 
	 * @param args
	 *            original sentence
	 * @return string sentence without space
	 */
	static String delleteSpaces(String args) {
		String temp = "";
		for (String s : args.split(" ")) {
			temp = temp + s;
		}
		return temp;
	}

	/**
	 * 
	 * @param arr
	 *            it's array of ready palindrome.
	 */
	static void show(String[] arr) {
		for (String str : arr) {
			if (str != null) {
				System.out.println(str);
			}
		}
	}
}
